# -*- coding: utf-8 -*-
__author__ = 'niimailtah'

from PyQt4.QtCore import *
from pluginspec import *

class PluginManager(QObject):
    def __init__(self):
        self.pluginSpecs = []

    # Plugin operations
    def setPluginPaths(self, paths):
        self.pluginPaths = paths
        self.readPluginPaths()

    def readPluginPaths(self):
        specFiles = []
        searchPaths = self.pluginPaths
        while len(searchPaths):
            _dir = QDir(searchPaths.pop(0))
            files = _dir.entryInfoList(["*.pluginspec",], QDir.Files)
            for _file in files:
                specFiles.append(str(_file.absoluteFilePath().toAscii()))
            dirs = _dir.entryInfoList(QDir.Dirs | QDir.NoDotAndDotDot)
            for subdir in dirs:
                searchPaths.append(str(subdir.absoluteFilePath().toAscii()))
        for specFile in specFiles:
            spec = PluginSpec()
            spec.read(specFile)
            self.pluginSpecs.append(spec)

    def loadQueue(self):
        return []

    def loadPlugins(self):
        queue = self.loadQueue()
        for spec in queue:
            self.loadPlugin(spec, pluginspec.Loaded)
        for spec in queue:
            self.loadPlugin(spec, pluginspec.Initialized)

    def loadPlugin(self, spec, destState):
        if destState == PluginSpec.Initialized:
            spec.initializePlugin()

    def plugins(self):
        return self.pluginSpecs

    def hasError(self):
        pass

    # Settings
    def setSettings(self, settings):
        pass

    def settings(self):
        pass

    def setGlobalSettings(self, settings):
        pass

    def globalSettings(self):
        pass

    def writeSettings(self):
        pass

    def profilingReport(self, what, spec):
        pass
